home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / robotparser.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  8KB  |  248 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. ''' robotparser.py
  5.  
  6.     Copyright (C) 2000  Bastian Kleineidam
  7.  
  8.     You can choose between two licenses when using this package:
  9.     1) GNU GPLv2
  10.     2) PSF license for Python 2.2
  11.  
  12.     The robots.txt Exclusion Protocol is implemented as specified in
  13.     http://info.webcrawler.com/mak/projects/robots/norobots-rfc.html
  14. '''
  15. import urlparse
  16. import urllib
  17. __all__ = [
  18.     'RobotFileParser']
  19.  
  20. class RobotFileParser:
  21.     ''' This class provides a set of methods to read, parse and answer
  22.     questions about a single robots.txt file.
  23.  
  24.     '''
  25.     
  26.     def __init__(self, url = ''):
  27.         self.entries = []
  28.         self.default_entry = None
  29.         self.disallow_all = False
  30.         self.allow_all = False
  31.         self.set_url(url)
  32.         self.last_checked = 0
  33.  
  34.     
  35.     def mtime(self):
  36.         '''Returns the time the robots.txt file was last fetched.
  37.  
  38.         This is useful for long-running web spiders that need to
  39.         check for new robots.txt files periodically.
  40.  
  41.         '''
  42.         return self.last_checked
  43.  
  44.     
  45.     def modified(self):
  46.         '''Sets the time the robots.txt file was last fetched to the
  47.         current time.
  48.  
  49.         '''
  50.         import time as time
  51.         self.last_checked = time.time()
  52.  
  53.     
  54.     def set_url(self, url):
  55.         '''Sets the URL referring to a robots.txt file.'''
  56.         self.url = url
  57.         (self.host, self.path) = urlparse.urlparse(url)[1:3]
  58.  
  59.     
  60.     def read(self):
  61.         '''Reads the robots.txt URL and feeds it to the parser.'''
  62.         opener = URLopener()
  63.         f = opener.open(self.url)
  64.         lines = [ line.strip() for line in f ]
  65.         f.close()
  66.         self.errcode = opener.errcode
  67.         if self.errcode in (401, 403):
  68.             self.disallow_all = True
  69.         elif self.errcode >= 400:
  70.             self.allow_all = True
  71.         elif self.errcode == 200 and lines:
  72.             self.parse(lines)
  73.         
  74.  
  75.     
  76.     def _add_entry(self, entry):
  77.         if '*' in entry.useragents:
  78.             self.default_entry = entry
  79.         else:
  80.             self.entries.append(entry)
  81.  
  82.     
  83.     def parse(self, lines):
  84.         '''parse the input lines from a robots.txt file.
  85.            We allow that a user-agent: line is not preceded by
  86.            one or more blank lines.'''
  87.         state = 0
  88.         linenumber = 0
  89.         entry = Entry()
  90.         for line in lines:
  91.             linenumber += 1
  92.             if not line:
  93.                 if state == 1:
  94.                     entry = Entry()
  95.                     state = 0
  96.                 elif state == 2:
  97.                     self._add_entry(entry)
  98.                     entry = Entry()
  99.                     state = 0
  100.                 
  101.             
  102.             i = line.find('#')
  103.             if i >= 0:
  104.                 line = line[:i]
  105.             
  106.             line = line.strip()
  107.             if not line:
  108.                 continue
  109.             
  110.             line = line.split(':', 1)
  111.             if len(line) == 2:
  112.                 line[0] = line[0].strip().lower()
  113.                 line[1] = urllib.unquote(line[1].strip())
  114.                 if line[0] == 'user-agent':
  115.                     if state == 2:
  116.                         self._add_entry(entry)
  117.                         entry = Entry()
  118.                     
  119.                     entry.useragents.append(line[1])
  120.                     state = 1
  121.                 elif line[0] == 'disallow':
  122.                     if state != 0:
  123.                         entry.rulelines.append(RuleLine(line[1], False))
  124.                         state = 2
  125.                     
  126.                 elif line[0] == 'allow':
  127.                     if state != 0:
  128.                         entry.rulelines.append(RuleLine(line[1], True))
  129.                         state = 2
  130.                     
  131.                 
  132.             line[0] == 'user-agent'
  133.         
  134.         if state == 2:
  135.             self.entries.append(entry)
  136.         
  137.  
  138.     
  139.     def can_fetch(self, useragent, url):
  140.         '''using the parsed robots.txt decide if useragent can fetch url'''
  141.         if self.disallow_all:
  142.             return False
  143.         url = self.allow_all if self.allow_all else '/'
  144.         for entry in self.entries:
  145.             if entry.applies_to(useragent):
  146.                 return entry.allowance(url)
  147.         
  148.         if self.default_entry:
  149.             return self.default_entry.allowance(url)
  150.         return True
  151.  
  152.     
  153.     def __str__(self):
  154.         return []([ str(entry) + '\n' for entry in self.entries ])
  155.  
  156.  
  157.  
  158. class RuleLine:
  159.     '''A rule line is a single "Allow:" (allowance==True) or "Disallow:"
  160.        (allowance==False) followed by a path.'''
  161.     
  162.     def __init__(self, path, allowance):
  163.         if path == '' and not allowance:
  164.             allowance = True
  165.         
  166.         self.path = urllib.quote(path)
  167.         self.allowance = allowance
  168.  
  169.     
  170.     def applies_to(self, filename):
  171.         if not self.path == '*':
  172.             pass
  173.         return filename.startswith(self.path)
  174.  
  175.     
  176.     def __str__(self):
  177.         if not self.allowance or 'Allow':
  178.             pass
  179.         return 'Disallow' + ': ' + self.path
  180.  
  181.  
  182.  
  183. class Entry:
  184.     '''An entry has one or more user-agents and zero or more rulelines'''
  185.     
  186.     def __init__(self):
  187.         self.useragents = []
  188.         self.rulelines = []
  189.  
  190.     
  191.     def __str__(self):
  192.         ret = []
  193.         for agent in self.useragents:
  194.             ret.extend([
  195.                 'User-agent: ',
  196.                 agent,
  197.                 '\n'])
  198.         
  199.         for line in self.rulelines:
  200.             ret.extend([
  201.                 str(line),
  202.                 '\n'])
  203.         
  204.         return ''.join(ret)
  205.  
  206.     
  207.     def applies_to(self, useragent):
  208.         '''check if this entry applies to the specified agent'''
  209.         useragent = useragent.split('/')[0].lower()
  210.         for agent in self.useragents:
  211.             if agent == '*':
  212.                 return True
  213.             agent = agent.lower()
  214.             if agent in useragent:
  215.                 return True
  216.         
  217.         return False
  218.  
  219.     
  220.     def allowance(self, filename):
  221.         '''Preconditions:
  222.         - our agent applies to this entry
  223.         - filename is URL decoded'''
  224.         for line in self.rulelines:
  225.             if line.applies_to(filename):
  226.                 return line.allowance
  227.         
  228.         return True
  229.  
  230.  
  231.  
  232. class URLopener(urllib.FancyURLopener):
  233.     
  234.     def __init__(self, *args):
  235.         urllib.FancyURLopener.__init__(self, *args)
  236.         self.errcode = 200
  237.  
  238.     
  239.     def prompt_user_passwd(self, host, realm):
  240.         return (None, None)
  241.  
  242.     
  243.     def http_error_default(self, url, fp, errcode, errmsg, headers):
  244.         self.errcode = errcode
  245.         return urllib.FancyURLopener.http_error_default(self, url, fp, errcode, errmsg, headers)
  246.  
  247.  
  248.